#include <stdio.h>
#include <pthread.h>

#define NUM (10)

int x[NUM] = {0};
int y = 0;

pthread_mutex_t m=PTHREAD_MUTEX_INITIALIZER;

void* race(void *unused) 
{
        int i, j;
        pthread_mutex_lock(&m);
        for(i=0; i<NUM; ++i) {
                ++x[i];
        }
        pthread_mutex_unlock(&m);
        ++ y;
        return NULL;
}

main() {
        pthread_t child;
        int i, j;

        if(pthread_create(&child, NULL, race, NULL) < 0)
                perror("pthread_create");
        pthread_mutex_lock(&m);
        for(i=0; i<NUM; ++i) {
                --x[i];
        }
        pthread_mutex_unlock(&m);

        -- y;

        if(pthread_join(child, NULL) < 0)
                perror("pthread_join");
        for(i=0; i<NUM; ++i)
                fprintf(stderr, "x[%d] = %d\n", i, x[i]);
        fprintf(stderr, "y = %d\n",y);
}
